package com.traffic.between.service;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;

import com.traffic.between.config.BetweenProperties;
import com.traffic.between.util.MD5Util;


@SuppressWarnings("deprecation")
public class ShuoRuanServiceImpl {
	private static Logger logger = Logger.getLogger(ShuoRuanServiceImpl.class);
	public static ShuoRuanServiceImpl service = new ShuoRuanServiceImpl();

	private static String HOST_ORDER_PATH = BetweenProperties.SHUORUAN_ORDER_URL;
	private static String HOST_ORDERSTATUS_PATH = BetweenProperties.SHUORUAN_ORDERSTATUS_URL;
	private static String SHUORUANFS_APPKEY = BetweenProperties.SHUORUANFS_APPKEY;
	private static String SHUORUANFS_SECURITYKEY = BetweenProperties.SHUORUANFS_SECURITYKEY;
	private static String SHUORUANQG_APPKEY = BetweenProperties.SHUORUANQG_APPKEY;
	private static String SHUORUANQG_SECURITYKEY = BetweenProperties.SHUORUANQG_SECURITYKEY;


	@SuppressWarnings({ "resource" })
	public String order(String productId, String phoneNo, String cstmOrderNo, String productPrice,
			String vasPid,String callbackURL) {
		
		net.sf.json.JSONObject json = new net.sf.json.JSONObject();
		String resData = "";
		try {
			net.sf.json.JSONObject params = new net.sf.json.JSONObject();
			json.put("action", "CZ");// 交易指令码[充值]
			json.put("orderId", cstmOrderNo);// 订单号
			json.put("chargeAcct", phoneNo);// 充值号码[手机、固话号码、QQ号码等
			json.put("chargeCash", productPrice);// 充值金额
			json.put("flowPackageSize", productId);// 充值号码[手机、固话号码、QQ号码等
			json.put("chargeType", 1);// 充值类型[0:手机充值，1:流量充值，2:固话充值，3:宽带充值]
			json.put("retUrl", callbackURL);
			String sign = "";
			if (vasPid.contains("ALL")) {
				json.put("flowPackageType", "0");// 0：国内月包 1:省内月包2：国内日包 3:省内日包
				params.put("agentAccount", SHUORUANQG_APPKEY);
				String jsonStr = json.toString().replaceAll("\\\\\\\\", "\\\\");
				sign = MD5Util.MD5(jsonStr + SHUORUANQG_SECURITYKEY);
			} else {
				json.put("flowPackageType", "1");// 0：国内月包 1:省内月包2：国内日包 3:省内日包
				String jsonStr = json.toString().replaceAll("\\\\\\\\", "\\\\");
				sign = MD5Util.MD5(jsonStr + SHUORUANFS_SECURITYKEY);
				params.put("agentAccount", SHUORUANFS_APPKEY);
			}

			params.put("sign", sign);
			params.put("busiBody", json);
			String paramsStr = params.toString().replaceAll("\\\\\\\\", "\\\\");
			logger.info("between- 硕软科技订购请求参数: params<>"+paramsStr);

			DefaultHttpClient httpClient = new DefaultHttpClient();
			HttpPost method = new HttpPost(HOST_ORDER_PATH);

			StringEntity entity = new StringEntity(paramsStr, "GBK");
			entity.setContentType("application/json");
			method.setEntity(entity);

			HttpResponse result = httpClient.execute(method);
			resData = EntityUtils.toString(result.getEntity(), "GBK");
			logger.info("硕软科技订购返回结果："+resData+",该订单号为："+cstmOrderNo);
			net.sf.json.JSONObject resultJson = net.sf.json.JSONObject.fromObject(resData);
			/**
			 * 下单成功确认编码：0：等待处理 1：暂停处理 2：正在处理 6：正在缴费 充值成功编码：11：处理成功 16：缴费成功
			 * 充值失败编码：20：取消处理 21：处理失败 26：缴费失败
			 * 
			 */
			String errorCode = String.valueOf(resultJson.get("errorCode"));
			// || "1".equals(errorCode) || "2".equals(errorCode) || "6".equals(errorCode)
			if ("1".equals(errorCode)) {// 订购中
				resData = "{\"resultCode\": \"00\",\"errorDesc\":" +"\""
						+ String.valueOf(resultJson.get("errorDesc")).toString() +"\""+"}";
			} else {// 订购失败
				resData = "{\"resultCode\": \"01\",\"errorDesc\":"+"\""
						+ String.valueOf(resultJson.get("errorDesc")).toString() + "\""+"}";
			}
			
		} catch (Exception e) {
			logger.info("硕软科技订购异常："+e.getMessage()+",该订单号为："+cstmOrderNo);
		}
		return resData;

	}

	@SuppressWarnings({ "resource" })
	public String orderStatus(String cstmOrderNo, String vasPid) {

		logger.info("between- 硕软科技订购状态请求参数: params cstmOrderNo = " + cstmOrderNo + ",vasPid= " + vasPid);

		net.sf.json.JSONObject json = new net.sf.json.JSONObject();
		String resData = "";
		try {
			net.sf.json.JSONObject params = new net.sf.json.JSONObject();
			json.put("action", "CX");// 交易指令码[充值]
			json.put("orderId", cstmOrderNo);// 订单号
			String sign = "";
			if (vasPid.contains("ALL")) {
				String jsonStr = json.toString().replaceAll("\\\\\\\\", "\\\\");
				params.put("agentAccount", SHUORUANQG_APPKEY);
				sign = MD5Util.MD5(jsonStr + SHUORUANQG_SECURITYKEY);
			} else {
				String jsonStr = json.toString().replaceAll("\\\\\\\\", "\\\\");
				params.put("agentAccount", SHUORUANFS_APPKEY);
				sign = MD5Util.MD5(jsonStr + SHUORUANFS_SECURITYKEY);
			}

			params.put("sign", sign);
			params.put("busiBody", json);
			String paramsStr = params.toString().replaceAll("\\\\\\\\", "\\\\");

			DefaultHttpClient httpClient = new DefaultHttpClient();
			HttpPost method = new HttpPost(HOST_ORDERSTATUS_PATH);

			StringEntity entity = new StringEntity(paramsStr, "GBK");
			entity.setContentType("application/json");
			method.setEntity(entity);

			HttpResponse result = httpClient.execute(method);
			resData = EntityUtils.toString(result.getEntity(), "GBK");
			logger.info("硕软科技状态查询返回结果："+resData+",该订单号为："+cstmOrderNo);
			net.sf.json.JSONObject resultJson = net.sf.json.JSONObject.fromObject(resData);
			/**
			 * 下单成功确认编码：0：等待处理 1：暂停处理 2：正在处理 6：正在缴费 充值成功编码：11：处理成功 16：缴费成功
			 * 充值失败编码：20：取消处理 21：处理失败 26：缴费失败
			 * 
			 */
			String errorCode = String.valueOf(resultJson.get("errorCode"));
			String orderStatuInt = String.valueOf(resultJson.get("orderStatuInt"));
			if ("1".equals(errorCode)) {
				if ("11".equals(orderStatuInt) || "16".equals(orderStatuInt)) {// 充值成功
					resData = "{\"resultCode\": \"00\",\"errorDesc\":"+"\""
							+ String.valueOf(resultJson.get("errorDesc")).toString() + "\"}";
				} else if ("20".equals(orderStatuInt) || "21".equals(orderStatuInt) || "26".equals(orderStatuInt)) {// 订购失败
					resData = "{\"resultCode\": \"01\",\"errorDesc\":"+"\""
							+ "订购失败" + "\"}";
				} else {// 订购中
					resData = "{\"resultCode\": \"03\",\"errorDesc\":"+"\""
							+ String.valueOf(resultJson.get("errorDesc")).toString() + "\"}";
				}
			} else {
				resData = "{\"resultCode\": \"03\",\"errorDesc\":"+"\""
						+ String.valueOf(resultJson.get("errorDesc")).toString() + "\"}";
			}
		
		} catch (Exception e) {
			logger.error("硕软科技订购状态请求异常",e);
			resData = "{\"resultCode\": \"03\",\"errorDesc\":" + "\"订购中" + "\"}";
		}
		return resData;

	}
}
